package com.atchangsha.system.service.impl;

import com.atchangsha.system.common.utils.Result;
import com.atchangsha.system.constant.PointConstants;
import com.atchangsha.system.dao.CustomerDao;
import com.atchangsha.system.dto.IodDTO;
import com.atchangsha.system.entity.CustomerEntity;
import com.atchangsha.system.service.CustomerService;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atchangsha.system.common.utils.PageUtils;
import com.atchangsha.system.common.utils.Query;
import com.atchangsha.system.dao.PointDao;
import com.atchangsha.system.entity.PointEntity;
import com.atchangsha.system.service.PointService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service("pointService")
public class PointServiceImpl extends ServiceImpl<PointDao, PointEntity> implements PointService {
    @Resource
    private CustomerDao customerDao;

    @Resource
    private PointDao pointDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PointEntity> page = this.page(
                new Query<PointEntity>().getPage(params),
                new QueryWrapper<PointEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public Result iod(IodDTO iodDTO) {
        int flag = iodDTO.getFlag();
        long point = iodDTO.getPoint();
        Long sum = iodDTO.getPointSum();
        Long customerId = iodDTO.getCustomerId();
        CustomerEntity customerEntity = customerDao.selectOne(new QueryWrapper<CustomerEntity>().eq("id", customerId));
        if (customerEntity != null) {
            //客户存在
            //得到积分表id
            Long pointId = customerEntity.getPointId();
            PointEntity pointEntity = pointDao.selectById(pointId);
            if (pointEntity == null) {
                pointEntity = new PointEntity();
                if (flag == 0){
                    return Result.fail(PointConstants.PointEnum.OPERATION_NOT_ALLOWED);
                }else {
                    pointEntity.setPoint(point);
                    pointEntity.setPointSum(sum);
                }
                int insert = baseMapper.insert(pointEntity);
                if (insert == 1) {
                    //插入成功
                    Long id = pointEntity.getId();
                    customerEntity.setPointId(id);
                    int length = customerDao.updateById(customerEntity);
                    if (length == 1) {
                        //更新成功
                        return Result.ok(PointConstants.PointEnum.UPDATE_SUCCESS).setResult(pointEntity);
                    }
                }
                //插入或者更新失败
                return Result.fail(PointConstants.PointEnum.UPDATE_FAIL);
            } else {
                long pointSum = pointEntity.getPointSum();
                Long pointEntityPoint = pointEntity.getPoint();
                if (flag == 0) {
                    pointSum -= sum;
                    pointEntityPoint -= point;
                } else if (flag == 1) {
                    pointEntityPoint += point;
                    pointSum += sum;
                }
                pointEntity.setPointSum(pointSum);
                pointEntity.setPoint(pointEntityPoint);
                int num = pointDao.updateById(pointEntity);
                if (num == 1) {
                    //积分更新成功
                    return Result.ok(PointConstants.PointEnum.UPDATE_SUCCESS).setResult(pointEntity);
                } else {
                    //积分更新失败
                    return Result.fail(PointConstants.PointEnum.UPDATE_FAIL);
                }
            }
        }
        return Result.fail(PointConstants.PointEnum.CUSTOMER_NOT_EXIST);
    }

}